package com.dldata.devtools30.controller.zhibiao;

import com.dldata.devtools30.entity.DevtModelMetadataTableEntity;
import com.dldata.devtools30.entity.DevtZbobjFieldEntity;
import com.dldata.devtools30.entity.DevtZbobjInfoEntity;
import com.dldata.devtools30.repository.metadata.DevtModelMetadataFieldDao;
import com.dldata.devtools30.repository.metadata.DevtModelMetadataTableDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbobjFieldEntityDao;
import com.dldata.devtools30.repository.zhibiao.DevtZbobjInfoEntityDao;
import com.dldata.devtools30.service.metadata.DevtModelMetadataTableService;
import com.dldata.devtools30.utils.Result;
import com.dldata.devtools30.utils.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/DevtZbobjFieldEntityController")
public class DevtZbobjFieldEntityController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    DevtZbobjFieldEntityDao zbobjFieldEntityDao;
    
    @Autowired
    DevtZbobjInfoEntityDao zbobjInfoEntityDao;
    
    @Autowired
    DevtModelMetadataTableDao devtModelMetadataTableDao;
    
    @Autowired
    DevtModelMetadataFieldDao devtModelMetadataFieldDao;
    
    @Autowired
    DevtModelMetadataTableService metadataTableService;

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAll(@RequestParam Map<String, String> paramMap){
        String zbobjid=StringUtil.isEmptyOrLength0(paramMap.get("zbobjid"))?"":paramMap.get("zbobjid");
        
        List<DevtZbobjFieldEntity> metadataTypeEntities = zbobjFieldEntityDao.getAllByZbObjId(zbobjid);

        return Result.ok(metadataTypeEntities);
    }
    
    @RequestMapping(value = "/getAllByModelId")
    @ResponseBody
    public Result getAllByModelId(@RequestParam Map<String, String> paramMap){
        String modelid=StringUtil.isEmptyOrLength0(paramMap.get("modelid"))?"":paramMap.get("modelid");
        
        List<DevtZbobjFieldEntity> metadataTypeEntities = zbobjFieldEntityDao.getAllByModelId(modelid);
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i < metadataTypeEntities.size(); i++) {
            DevtZbobjFieldEntity devtZbobjFieldEntity = metadataTypeEntities.get(i);
            stringBuilder.append(devtZbobjFieldEntity.getZbobjname());
            stringBuilder.append(",");
        }
        if(stringBuilder.length()>0){
            stringBuilder.deleteCharAt(stringBuilder.length()-1);
        }
        return Result.ok("查询成功!",stringBuilder.toString());
    }


    @RequestMapping(value = "/save")
    @ResponseBody
    public Result save(DevtZbobjFieldEntity entity){
        if(StringUtil.isEmptyOrLength0(entity.getZbobjid())){
            return Result.error("指标对象不能为空");
        }
        DevtZbobjInfoEntity devtZbobjInfoEntity = zbobjInfoEntityDao.findById(entity.getZbobjid()).orElse(null);
        List<DevtModelMetadataTableEntity> metadataTableEntities = metadataTableService.findById(entity.getModelid());
        DevtModelMetadataTableEntity devtModelMetadataTableEntity=null;
        if(metadataTableEntities.size()>0){
            devtModelMetadataTableEntity = metadataTableEntities.get(0);
        }

//        entity.setZbobjid();
//        entity.getZbobjid()
        
        entity.setZbobjcode(devtZbobjInfoEntity.getZbobjcode());
        entity.setZbobjname(devtZbobjInfoEntity.getZbobjname());
        
        entity.setModelid(devtModelMetadataTableEntity.getId());
        entity.setModelcode(devtModelMetadataTableEntity.getCode());
        entity.setModelname(devtModelMetadataTableEntity.getName());
    
//        entity.setModelfieldcode();
//        entity.setModelfieldname();
        
        if(StringUtil.isEmptyOrLength0(entity.getId())){
            entity.setId(UUID.randomUUID().toString());
        }
        zbobjFieldEntityDao.save(entity);

        return Result.ok();
    }
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(@RequestParam Map<String, String> paramMap){

        String id =paramMap.get("id");
        if(StringUtil.isEmptyOrLength0(id)){
            return  Result.error("参数有误");
        }
        try {
            DevtZbobjFieldEntity zbobjInfoEntity = zbobjFieldEntityDao.findById(id).orElse(null);
            if(zbobjInfoEntity!=null){
                zbobjFieldEntityDao.delete(zbobjInfoEntity);
            }
        }
        catch (Exception ex){
            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage(),ex);
            return  Result.error("操作错误，请稍候再试!");
        }
        return  Result.ok("删除成功");
    }
    
    
    @RequestMapping(value = "/getTableAll")//46d614f9-f79b-459c-97bf-4053d8b06730
    @ResponseBody
    public Result getTableAll(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam Map<String, Object> param) {
        
        List<DevtModelMetadataTableEntity> tableEntities= metadataTableService.getAll();
        List<Map<String,Object>> mapList = new ArrayList<>();
        tableEntities.stream().forEach(
            m->{
                Map<String,Object> data=new HashMap<>();
                data.put("id",m.getId());
                data.put("tableid",m.getId());
                data.put("name",m.getName());
                data.put("code",m.getCode());
                mapList.add(data);
        });
        return Result.ok(mapList);
    }
    
    
    @RequestMapping("edit")
    public String toEdit(String zbobjid, Model model){
        model.addAttribute("zbobjid",zbobjid);
        return "thymeleaf/devtools30/zhibiao/v_zhibiaoobjfieldEdit";
    }
}
